<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 14:25
 */

namespace app\index\controller;


class All extends Base
{
    /*
     * 获取nID_
     */
    public function nID_($func)
    {
        if ($this->redis->_exists("nID_")) {
            $nID_ = $this->redis->_get("nID_");
            $func($nID_);
        } else {
            $this->lDService->nID_(function ($b, $msg = "") use ($func) {
                if ($b) {
                    $nID_ = $this->redis->_get("nID_");
                    $func($nID_);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }

    /*
     * 获取每次开镜的数据
     */
    public function luckNum($nID_, $func)
    {
        $key = "luckNum_" . $nID_;
        if ($this->redis->_exists($key)) {
            $data = $this->redis->_get($key);
            $func($data);
        } else {
            $this->lDService->luckNum($nID_, function ($b, $msg = "") use ($key, $func) {
                if ($b) {
                    $data = $this->redis->_get($key);
                    $func($data);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }

    /*
     * 获取所有的幸运数据
     */
    public function luckNumAll()
    {
        // 获取nID_
        $body = [];

        $this->contract->call('nID_', function ($err, $result) use (&$body) {
            if ($result) {
                $nums = intval($result[0]->toString());
                $this->redis->_set("nID_", $nums, 120);
                for ($i = 1; $i < $nums; $i++) {
                    // 获取每期幸运数字
                    $this->contract->call("numLucks_", $i, function ($err, $res) use (&$body, $i, $nums) {
                        if ($res) {
                            $body[$i]["num"] = intval($res[0]->toString());
                            // 获取每期开奖金额
                            $this->contract->call("numPot_", $i, function ($err, $res) use (&$body, $i, $nums) {
                                if ($res) {
                                    $body[$i]['pot'] = intval($res[0]->toString());
                                    if ((count($body) + 1) == $nums) {
                                        $this->api_export($body);
                                    }
                                } else {
                                    $this->api_error("20000", $err->getMessage());
                                }
                            });
                        } else {
                            $this->api_error("20000", $err->getMessage());
                        }
                    });
                }
            } else {
                $this->api_error("20000", $err->getMessage());
            }
        });
    }

    /*
     * 上次开奖时间
     */
    public function lastPotTime($func)
    {
        if ($this->redis->_exists("lastPotTime")) {
            $lastPotTime = $this->redis->_get("lastPotTime");
            $func($lastPotTime);
        } else {
            $this->lDService->lastPotTime(function ($b, $msg = "") use ($func) {
                if ($b) {
                    $lastPotTime = $this->redis->_get("lastPotTime");
                    $func($lastPotTime);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }


    /*
     * 奖池金额 & eth总投资
     */
    public function pot($func)
    {
        if ($this->redis->_exists("pot")) {
            $pot = $this->redis->_get("pot");
            $func($pot);
        } else {
            $this->lDService->pot(function ($b, $msg = "") use ($func) {
                if ($b) {
                    $pot = $this->redis->_get("pot");
                    $func($pot);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }

    /*
     * 用户的地址对应的ID (用户地址对应的ID)
     */
    public function pIDxAddr()
    {
        $address = input('address');
        if ($this->redis->_exists($address)) {
            $pID = $this->redis->_get($address);
            $this->api_export($pID);
        } else {
            $this->lDService->pIDxAddr($address, function ($b, $msg = "") use ($address) {
                if ($b) {
                    $pID = $this->redis->_get($address);
                    $this->api_export($pID);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }

    public function plyr($func)
    {
        $pID = input('pID');
        $key = "plyr_" . $pID;
        if ($this->redis->_exists($key)) {
            $data = $this->redis->_get($key);
            $func($data);
        } else {
            $this->lDService->plyr($pID, function ($b, $msg = "") use ($key, $func) {
                if ($b) {
                    $data = $this->redis->_get($key);
                    $func($data);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }

    public function ethPrice($func)
    {
        if ($this->redis->_exists("ethPrice")) {
            $data = $this->redis->_get("ethPrice");
            $func($data);
        } else {
            $CNY = json_decode($this->get_curl("https://api.coinbase.com/v2/prices/ETH-CNY/spot"), true);
            $USD = json_decode($this->get_curl("https://api.coinbase.com/v2/prices/ETH-USD/spot"), true);
            $data = [
                "CNY" => $CNY['data']['amount'],
                "USD" => $USD['data']['amount']
            ];
            $this->redis->_set("ethPrice", $data, 600);
            $func($data);
        }
    }

    /*
     * 主接口，baseInfo
     */
    public function baseInfo()
    {
        $body = [];
        //当前轮次
        $this->nID_(function ($res) use ($body) {
            $body["rID_"] = $res;
            //上一轮幸运数字
            $this->luckNum($body["rID_"] - 1, function ($res) use ($body) {
                $body["luckNum"] = $res;
                // 倒计时
                $this->lastPotTime(function ($res) use ($body) {
                    $body["lastPotTime"] = $res;
                    // 奖池 & 总投资 & 总keys
                    $this->pot(function ($res) use ($body) {
                        $body["pot"] = $res;
                        // eth 价格
                        $this->ethPrice(function ($res) use ($body) {
                            $body["ethPrice"] = $res;
                            $this->api_export($body);
                        });
                    });
                });
            });
        });
    }

    /*
     * 主接口，幸运数字
     */
    public function luckNumInfo()
    {
        $nID_ = input("nID_");
        $key = "luckNum_" . $nID_;
        if ($this->redis->_exists($key)) {
            $data = $this->redis->_get($key);
            $this->api_export($data);
        } else {
            $this->lDService->luckNum($nID_, function ($b, $msg = "") use ($key) {
                if ($b) {
                    $data = $this->redis->_get($key);
                    $this->api_export($data);
                } else {
                    $this->api_error("20000", $msg);
                }
            });
        }
    }
}